﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Restricted_CreatedateSite : System.Web.UI.Page
{
    private aspnet_User currentUser;
    private DatenightBLL datenightBLL;
    private PersonBLL personBLL;
    private CityBLL cityBLL;

    protected void Page_Load(object sender, EventArgs e)
    {
        datenightBLL = DatenightBLL.Instance;
        personBLL = PersonBLL.Instance;
        cityBLL = CityBLL.Instance;
        currentUser = personBLL.getCurrentUser(Membership.GetUser());

        CalExtender.StartDate = DateTime.Today;
        CalExtender.EndDate = DateTime.Today.AddDays(14);

        if (currentUser.GenderId == 1)
        {
            DateGender.SelectedIndex = 2;
        }
        else if (currentUser.GenderId == 2)
        {
            DateGender.SelectedIndex = 1;
        }
        else
        {
            DateGender.SelectedIndex = 0;
        }

        FillAge();
    }

    private void FillAge()
    {
        for (int i = 15; i <= 99; i++)
        {
            ListItem AgeFrom = new ListItem();
            AgeFrom.Text = i.ToString();
            DateAgeFrom.Items.Add(AgeFrom);
        }
        for (int i = 99; i >= 15; i--)
        {
            ListItem AgeTo = new ListItem();
            AgeTo.Text = i.ToString();
            DateAgeTo.Items.Add(AgeTo);
        }
    }

    //Method to get autocompletedate from database
    [System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod]
    public static string[] GetCompletionListCity(string prefixText, int count, string contextKey)
    {
        return DatenightBLL.Instance.getCitysForCompletionlist(prefixText).ToArray();
    }

    protected void CreateDate_Click(object sender, EventArgs e)
    {
        
        if (datenightBLL.DatesCreatedGreaterThanOne(currentUser))
        {
            WarningLabel.Text = "Du har allerede oprettet en date";
        }
        else
        {

            Datenight datenight = new Datenight();
            datenight.Username = currentUser.UserName;
            WarningLabel.Text = "";

            //Extract city and zipnumber from textbox
            try
            {
                string cityname = CityZipcodeTxt.Text;
                cityname = cityname.Substring(0, cityname.Length - 7);
                string zipcode = CityZipcodeTxt.Text;
                zipcode = zipcode.Substring(zipcode.Length - 5, 4);
                var CityObj = cityBLL.FindCityByNameAndZipcode(cityname, zipcode);
                datenight.DatePlace = CityObj.CityId;


                //Insert date from textbox on website
                try
                {
                    CultureInfo provider = CultureInfo.InvariantCulture;
                    DateTime date = DateTime.ParseExact(DateDate.Text, CalExtender.Format, provider);
                    datenight.Date = date;

                    datenight.GenderId = DateGender.SelectedIndex;
                    datenight.WhoPay = DatePersonToPay.SelectedValue;
                    datenight.Agemin = Convert.ToInt32(DateAgeFrom.SelectedValue);
                    datenight.Agemax = Convert.ToInt32(DateAgeTo.SelectedValue);
                    if (DateDescription.Text == null || DateDescription.Text.Length > 199)
                    {
                        datenight.Description = DateDescription.Text;

                        datenightBLL.createDatenight(datenight);

                        Page.Response.Redirect("~/Restricted/SearchSite.aspx?datecreatesucces=true", false);
                    }
                    else
                    {
                        WarningLabel.Text = "Beskrivelse skal være på minimum 200 tegn";
                    }
                    
                }
                catch(Exception ee)
                {
                    WarningLabel.Text = "Datofelt er ikke udfyldt korrekt eller i forkert format" + ee.Message;
                }

            }
            catch(Exception)
            {
                WarningLabel.Text = "By/postnr felt er ikke gyldigt eller i forkert format";

            }

            

            
        }
    }
}